var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        lists: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            keywords: '',
            status: 0, //状态（0-通知我的 1-我发起的)
            IsRead: false,//所有 2 未读0
        },
        current_item_length: 0,
        total: {
            StartNumber: 0,//通知我的
            SendNumber: 0,//我发起的
        },
        can_scroll: false,
        showsearch: false,
    },
    methods: {
        doIsRead: function () {
            var that = this;
            that.form.IsRead = !that.form.IsRead;
            that.get_data();
        },
        get_data: function () {
            var that = this;
            if (that.form.status == 0) {
                that.get_first();
            }
            if (that.form.status == 1) {
                that.get_seconed();
            }
        },
        get_first: function () {
            var that = this;
            that.can_scroll = false;
            var options = {};
            options.Keywords = that.form.keywords;
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.Initiated = 0;
            options.IsRead = that.form.IsRead ? 0 : 2;
            options.action = "getinternallist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.data.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.data;
                    } else {
                        that.list = that.list.concat(data.data);
                    }
                    app.current_item_length = that.list.length;
                    that.total.StartNumber = data.NotifyMe;
                    // that.total.SendNumber = data.summary.SendNumber;
                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        get_seconed: function () {
            var that = this;
            that.can_scroll = false;
            var options = {};
            options.Keywords = that.form.keywords;
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.Initiated = 1;
            options.IsRead = that.form.IsRead ? 0 : 2;
            options.action = "getmyinternallist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.data.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.lists = data.data;
                    } else {
                        that.lists = that.lists.concat(data.data);
                    }
                    app.current_item_length = that.lists.length;
                    that.total.SendNumber = data.NotifyMe;
                } else if (err) {
                    that.lists = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        select_status: function (status) {
            var that = this;
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.form.status = status;
            that.list = [];
            that.lists = [];
            that.get_data();
        },
        do_search: function () {
            this.get_data();
        },
        //新增内部通知
        do_add_neibu: function () {
            ns.openWin('neibutz_add', '新增内部通知', {});
        },
        openDetail: function (item) {
            var that = this;
            ns.openWin('neibutz_detail_frm', '内部通知详情', {
                id: item.ID,
                ids: item.IDs || 0,
                IsRead: item.IsRead,
                TypeStatus: that.form.status
            });
        },

        select_allstatus: function () {
            var that = this;
            that.showsearch = !that.showsearch;
        },
        doHideSearch: function () {
            var that = this;
            that.showsearch = false;
        },
        // 确认收到
        do_getok: function (item) {
            var that = this;
            //调用接口
            var options = {};
            options.IDs = item.IDs;
            options.tdRemark = "收到";
            options.action = "savanotificationread";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status) {
                        that.get_data();
                        api.toast({
                            msg: "操作成功！",
                            duration: 2000,
                            location: 'bottom'
                        });
                        return;
                    }
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        //发布
        do_release: function (item) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定发布该内部通知',
                buttons: ['确定', '取消']
            }, function (ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        //调用接口
                        var options = {};
                        options.ID = item.ID;
                        options.action = "publishinternalnotification";
                        ns.post(options, function (succeed, data, err) {
                            if (succeed) {
                                api.toast({
                                    msg: data.Msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                                that.get_data();
                            } else if (err) {
                                api.toast({
                                    msg: err,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            }
                        });
                    };
                };
            });
        },

    }
});

apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();

    ns.init_pull();
    ns.init_search();
    // app.get_data();
    app.get_first();
    app.get_seconed();

    api.addEventListener({
        name: 'reloadneibulist'
    }, function (ret, err) {
        app.get_first();
        app.get_seconed();
    });

    api.addEventListener({
        name: 'do_add_neibu'
    }, function (ret, err) {
        app.do_add_neibu();
    });
};


